package com.redtree.air.db
{
	import com.redtree.air.sqlite.Query;
	import com.redtree.air.sqlite.SQLite;
	
	import flash.data.SQLStatement;
	import flash.data.SQLTransactionLockType;
	import flash.errors.SQLError;
	import flash.filesystem.*;
	
	import mx.controls.Alert;
	import mx.controls.dataGridClasses.DataGridColumn;
	
	public class Model extends Query
	{
		public static var instance:Model = new Model();
		public const DB_TYPE_SQLITE:String="data.db";
		public const SQL_FILE_CREATE_TABLE:String="data.sql";
		public const DEFAULT_PAGE_SIZE:int=15;
		private static var _currentSecurityLevel:int = 1;
		public var dataGridColumnsAry:Array = new Array();
		public var tableName:String = '';

		
		public function Model()
		{
			var file:File = File.applicationDirectory.resolvePath(DB_TYPE_SQLITE);
			var fileExists:Boolean = file.exists;
			var sqlite:SQLite = new SQLite();
			sqlite.file = DB_TYPE_SQLITE;			
			connection = sqlite.connection;
			if (!fileExists)
			{
				createTables(SQL_FILE_CREATE_TABLE);
			}
		}

		public function query(sqlString:String, ownTransaction:Boolean=true):Boolean
		{
			trace(sqlString);
			sql = sqlString;
			return execute(ownTransaction);

		}
		/**
		 * 返回表名
		 */
		protected function getTableName( tblName ){
			return tblName;
		}
		/**
		 * 更新表的字段
		 */
		public function updateField( id:int, field:String, value:String ):void
		{
			var sql:String = "UPDATE "+tableName+" set "+ field+'="'+value+'" WHERE id='+id;
			query(sql);
		}
		private function createTables(fileName:String):void
		{
			var file:File = File.applicationDirectory.resolvePath(fileName);
			var fileStream:FileStream = new FileStream();
			fileStream.open(file,FileMode.READ);
			var str:String = fileStream.readMultiByte(file.size,File.systemCharset);
			fileStream.close();
			var stmts:Array = str.split(';');
			connection.begin(SQLTransactionLockType.IMMEDIATE);
			for ( var i:int=0; i<stmts.length; i++ ){				
				if( stmts[i] && stmts[i] != '' ){
					if( !query(stmts[i], false) ){
						connection.rollback();
						return ;
					}					
				}
			}
			connection.commit();
		}

		public static function get currentSecurityLevel():int
		{
			return _currentSecurityLevel;
		}

		public static function set currentSecurityLevel(value:int):void
		{
			_currentSecurityLevel = value;
		}
		public function getDataGridColumns():Array
		{
			var column:DataGridColumn;
			var columns:Array = new Array();
			for( var i:int=0; i<dataGridColumnsAry.length; i++ )
			{
				column = new DataGridColumn();
				column.dataField = dataGridColumnsAry[i][0];
				column.headerText = dataGridColumnsAry[i][1];
				columns.push(column);
			}
			return columns;
		}
		/**
		 * 删除
		 */
		public function deleteElement( id:int ):Boolean
		{
			var sql:String = "delete from "+tableName+" where id = "+id;
			return Model.instance.query(sql);
		}
		/**
		 * 添加
		 */
		public function addElement( name:String, securityLevel:int ):Boolean
		{
			var sql:String = "insert into "+tableName+" ( name, security_level ) values "+'("'+name+'", '+securityLevel+")";
			return Model.instance.query(sql);
		}

	}
}